// pages/marketing/newproductlau/newproductlau.js
import { smsHomeNewProduct } from '../../../../config/config'
var configUrl = require('../../../../config/config')  //引入配置接口文件
//获取应用实例
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl:getApp().globalData.imgUrl,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    page: 1,
    rows: 10,
    nomoreData: false,
    listData: [],
    addrCode: {},
    titleHeight: '',
    /**-合伙人--*/
    isShowPartnerAll: 1,
    showPartnerCodePop: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (!wx.getStorageSync('loginMsg').token) {
      app.initLogin() //初始化登录
    }
    let that = this
    wx.getSystemInfo({
      success (res) {
        let statusBarHeight = res.statusBarHeight
        that.setData({
          titleHeight: statusBarHeight + 'px'
        })
      }
    })
    let addrCode = wx.getStorageSync('addrCode')
    that.setData({
      addrCode: addrCode
    })
    that.getsmsHomeNewProduct()
    if (options.hid) {
      var scene = decodeURIComponent(options.hid) //参数二维码传递过来的参数
        var stInFun = setInterval(() => {
          if (wx.getStorageSync('loginMsg').token && wx.getStorageSync('addrCode').provinceCode) {
            clearInterval(stInFun)
            if (!wx.getStorageSync('loginMsg').loginState) {
              if (wx.getUserProfile) {
                app.getUserProfile(function(){
                  that.setData({
                    getPhoneNumberData:{
                      isShowGetPhoneNumber:true
                    }
                  })
                })
              } else {
                if(!wx.getStorageSync('loginMsg').bindingPhone){
                  that.setData({
                    getPhoneNumberData:{
                      isShowGetPhoneNumber:true
                    }
                  })
                }
              }
              // this.setData({
              //   isShowGetUserInfo: true
              // })
            }
            const reloadListFlag = true
            that.getsmsHomeNewProduct(reloadListFlag)
            app.bindingPartner(scene)
            this.getPartnerInfo(scene)
          }
        }, 1000)
    }
  },
  bindGetUserInfo: function (e) { //登录-注册
    var that=this;
    app.getUserInfo()
    this.setData({
      showSetting: false,
      isShowGetUserInfo: false
    })
    var setFun=setInterval(()=>{
      if(wx.getStorageSync('showGetPhone')){
        that.setData({
          getPhoneNumberData:{
            isShowGetPhoneNumber:true,
          },
          showSetting: false,
          isShowGetUserInfo: false
        })
        wx.removeStorageSync('showGetPhone')
        clearInterval(setFun)
      }
    },1000)
    
  },
  onCloseShowGetUserInfo() { //关闭弹出层
    this.setData({
      showSetting: false,
      isShowGetUserInfo: false
    })
  },
   /**绑定手机号 ---str*/
   closeGetphone(){
    this.setData({
      getPhoneNumberData:{
        isShowGetPhoneNumber:false

      }
    })
  },
  getPhoneNumber(e){
    var that=this
    let loginMsg = wx.getStorageSync('loginMsg')
    if (e.detail.iv && e.detail.encryptedData) {
      let option = {}
      option.encryptedData = e.detail.encryptedData
      option.iv = e.detail.iv
      option.openid = loginMsg.openid
      option.session_key = loginMsg.session_key
      app.reqFun.requestJson('POST',configUrl.decryptGetPhone,option,(res)=>{
        if(res.data.success==1){
          that.setData({
            getPhoneNumberData:{
              isShowGetPhoneNumber:false
            }
          })
          that.onCloseShowGetUserInfo();
          wx.showToast({
            title: '绑定成功。',
          })
        }
      }) 
    } else {
      console.log('取消')
    }
  },
  /**  end
   *  绑定手机号
   */
// 新品首发
getsmsHomeNewProduct(reloadListFlag) {
  let that = this
  let option = {}
  option.page = that.data.page
  option.rows = that.data.rows
  option.callFrom = 'NEW'
  option.provinceCode = that.data.addrCode.provinceCode
  app.reqFun.requestJson('POST',smsHomeNewProduct,option,(res)=>{
    if(res.data.success==1){
      console.log('new', res.data.data.solrProductDtos)
      let records = res.data.data.solrProductDtos.records
      if (records.length < that.data.rows) {
        that.setData({
          nomoreData: true
        })
      }
      if (reloadListFlag) {
        that.setData({
          listData: records
        })
      } else {
        that.setData({
          listData: that.data.listData.concat(records)
        })
      }
    }
  })
},
goreBack(e){
  wx.navigateBack({
    delta: 1,
  })
},
// 跳转商品详情
goGoodsInfo(option) {
  let skuid = option.currentTarget.dataset.skuid
  console.log('skuid', skuid)
  wx.navigateTo({
      url: '/packageShop/pages/goodsInfo/goodsInfo?id=' + skuid,
  })
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log("刷新")
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("到底部")
    if (!this.data.nomoreData) {
      this.setData({
        page: this.data.page + 1
      })
      this.getsmsHomeNewProduct()
    }
  },
  goHome: function (e) { //返回主页
    wx.reLaunch({
      url: '/pages/newIndex/newIndex'
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '新品首发',
      desc: '新品首发',///packagBuy/pages/marketing/newProduct/newProduct?id=760215991442472960
      path: '/packagBuy/pages/marketing/newProduct/newProduct?hid='+wx.getStorageSync('loginMsg').userId
     }
  },
        //获取合伙人信息
        getPartnerInfo:function(userId){
          console.log('获取合伙人信息')
          console.log(userId)
          let that = this;
          app.reqFun.requestJson('get', configUrl.getPartnerInfo + userId, '', (res) => {
            console.log(res.data)
            if (res.data.success == 1) {
              var partnerInfo={
                id:wx.getStorageSync('loginMsg').userId,
                partnerNickName:res.data.data.partnerNickName,
                partnerPhone:res.data.data.partnerPhone,
                partnerPhoto:res.data.data.partnerPhoto,
                partnerQrCode:res.data.data.partnerQrCode,
              }
              that.setData({
                partnerInfo: partnerInfo
              })
            }
          }, true)
        },
        //展开合伙人信息
        showPartnerAll: function () {
          let isShowPartnerAll;
          if (this.data.isShowPartnerAll == 1) {
            isShowPartnerAll = 0;
          } else {
            isShowPartnerAll = 1;
          }
          this.setData({
            isShowPartnerAll: isShowPartnerAll,
          })
        },
        //展示合伙人二维码
        partnerCode: function (e) {
          // this.setData({
          //   showPartnerCodePop: true,
          // })
          let codeImg=e.target.dataset.url
          wx.previewImage({
            urls:[codeImg],
            current: codeImg, // 当前显示图片的http链接
           })
        },
         //关闭合伙人二维码
        closeCodePop:function(){
          console.log("3253456678dsgudkdkgfksfvkjdsvbjkdsvbvkj")
          this.setData({
            showPartnerCodePop: false,
          })
        },
        //拨打合伙人电话
        partnerPhone:function(){
          let that = this;
          wx.makePhoneCall({
            phoneNumber: that.data.partnerInfo.partnerPhone,
          })
        }
})